/******************************************************************************
 * 
 * Announce: Meteorological Toolkit (MTK)
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://gitee.com/shenyczz/csharp_mtk
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/
using System;
using System.Drawing;

namespace CSharpKit.Palettes
{
    public class SegmentPalette2 : PaletteBase
    {
        public SegmentPalette2()
            : this("N/A") { }

        public SegmentPalette2(string unit)
            : base(unit)
        {
            this.PaletteType = PaletteType.Segment2;
        }

        /// <summary>
        /// todo: 做到分段。。。
        /// </summary>
        /// <param name="value"></param>
        /// <param name="colorDefault"></param>
        /// <returns></returns>
        public override Color GetColor(Double value, Color colorDefault)
        {
            Color result = colorDefault;

            // 无效的浮点值(-9999)
            if (System.Math.Abs(value - KitConstants.InvalidValue) < 1.0e-12)
            {
                return colorDefault;
            }

            for (var i = 0; i < this.Count; i++)
            {
                IPaletteItem item = this[i];

                if ((value >= item.Value) && (value < item.Value2))
                {
                    result = item.Color;

                    if (i <= this.Count - 2)
                    {
                        var c1 = this[i + 0].Color;
                        var c2 = this[i + 1].Color;

                        var v1 = item.Value;
                        var v2 = item.Value2;
                        var vt = (value - v1) / (v2 - v1);

                        var r = (byte)(c1.R + (c2.R - c1.R) * vt);
                        var g = (byte)(c1.G + (c2.G - c1.G) * vt);
                        var b = (byte)(c1.B + (c2.B - c1.B) * vt);

                        // 线性插值结果
                        // result = Color.FromArgb(r, g, b);
                    }

                    break;
                }

            }

            return result;
        }


        public new SegmentPalette2 Clone()
        {
            SegmentPalette2 palette = new SegmentPalette2();
            this.Clear();

            foreach (PaletteItem item in Items)
            {
                palette.Add(item);
            }

            return palette;
        }

        //}}@@@
    }


}
